import {throttle} from '@/libs/debounce'

type SearchValue = string | number | undefined

interface UseSearchOption {
  inputCallBack?: () => void
  defaultValue: SearchValue
}

function useSearch({inputCallBack, defaultValue = undefined}: UseSearchOption) {
  const searchValue = ref<SearchValue>(defaultValue)

  const setSearchValue = (data: SearchValue) => {
    searchValue.value = data
  }

  const searchInput = throttle(
    inputCallBack
      ? inputCallBack
      : () => {
          console.error('搜索栏未配置input时间的回调函数！')
        },
    300
  )

  return {
    searchValue,
    setSearchValue,
    searchInput
  }
}

export default useSearch
